package com.app.controller.backstage;

import com.app.mapper.CategoryMapper;
import com.app.model.AdminUserModel;
import com.app.model.CategoryModel;
import com.app.model.VillageFoodModel;
import com.app.service.AdminUserService;
import com.app.service.VillageFoodService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 美⻝乡村
 */
@Controller
@RestController
@RequestMapping("/admin")
public class AdminVillageFoodController {
    @Autowired
    private VillageFoodService villageFoodService;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AdminUserService adminUserService;

    //类型列表
    @GetMapping("/foodTypeList")
    public Result<Object> foodTypeList(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 3);
        return Result.success(categoryMapper.selectList(queryWrapper));
    }

    //新增和编辑
    @PostMapping("/insertAndUpdateFood")
    public Result<Object> insertAndUpdateVideo(@RequestBody VillageFoodModel model, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if (model.getId() > 0) {
            model.setUpdateTime(CommonHelp.getNowTimestamp());
            villageFoodService.update(model);
        } else {
            model.setCreateTime(CommonHelp.getNowTimestamp());
            villageFoodService.insert(model);
        }
        return Result.success("操作成功");
    }

    //删除
    @GetMapping("/deleteFood")
    public Result<Object> deleteVideo(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        villageFoodService.deleteById(id);
        return Result.success("操作成功");
    }

    //列表
    @GetMapping("/getFoodList")
    public Result<Object> getVideoList(Integer page, Integer limit, String keyword, Integer type, Integer township, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return Result.success(villageFoodService.getList(page, limit, keyword, type, null, null, township));

    }

    //查看详情
    @GetMapping("/getFoodInfo")
    public Result<Object> getVideoInfo(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return Result.success(villageFoodService.selectById(id));
    }

    //更新状态
    @GetMapping("/examineFood")
    public Result<Object> examineFood(Integer id, Integer status, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        VillageFoodModel model = new VillageFoodModel();
        model.setId(id);
        model.setStatus(status);
        villageFoodService.update(model);
        return Result.success("更新成功");
    }
}
